<?php
/*
 * class控制器  
 * Tobey
 * */
require_once "baseController.class.php";
class classController extends baseController{

	/**
	 * GET请求   打开班级管理页面
     * ------------------------------------------------------------------------  
	 * ajax请求   获取所有班级信息
     */
	public function classes() {
		if(IS_GET) {
            //及时归档处理
			D()->table(array("class"=>""))->where(array("is_filed"=>0,"cla_end"=>array('<', date('Y-m-d'))))->update(array("is_filed"=>1));
			$this->display("Class/classes");
		} else if(IS_AJAX && IS_LOCAL){
            session_start();
            $sql = D();            //连接数据库
			if($_SESSION["isTeacher"] == "true"){			//获取教师的
                $where["cou.teacher_tea_id"] = $_SESSION["id"];
                $where["cla.is_filed"] = 0;
                $where["cou.is_available"] = 1;
                $where["__string__"] = "cla.cou_id = cou.cou_id";
                $classes = $sql->table(array("class"=>"cla","course"=>"cou"))->where($where)->order("cla.cla_start desc")->field(array("cla.cla_id", "cla.cla_name", "cla.cla_stu_num", "cla.cla_start", "cla.cla_end","cla.is_auto", "cou.cou_name", "cou.cou_id"))->select();
                unset($where);
                if($classes){
                    $class_len = count($classes);
                    for($i = 0;$i<$class_len;$i++){
                        $classes[$i]["quiz"] = $sql->table(array("question_set"=>""))->where(array("class_cla_id"=>$classes[$i]["cla_id"],"qus_status"=>array("in",array(0,1,2))))->field(array("qus_id", "qus_name"))->order("qus_create_time desc")->limit(0,5)->select();
                    }
                    $data = $this->message(1,"获取班级信息成功!",$classes);
                    unset($classes,$class_len);
                } else{
                    $data = $this->message(0,"无可用班级!","");
                }
            } else{			//获取学生的
                $where["cs.student_stu_id"] = $_SESSION["id"];
                $where["cs.is_pass"] = 1;
                $where["cla.is_filed"] = 0;
                $where["cou.is_available"] = 1;
                $where["__string__"] = "cs.class_cla_id = cla.cla_id and cla.cou_id = cou.cou_id";
                $classes = $sql->table(array("class_student"=>"cs","class"=>"cla","course"=>"cou"))->where($where)->field(array("cla.cla_id","cla.cla_code", "cla.cla_name", "cla.cla_stu_num", "cla.cla_start", "cla.cla_end","cla.is_auto" ,"cou.cou_name", "cou.cou_id"))->order("cla.cla_start desc")->select();
                if($classes){
                    $class_len = count($classes);
                    for($i = 0;$i<$class_len;$i++){
                        $classes[$i]["quiz"] = $sql->table(array("question_set"=>""))->where(array("class_cla_id"=>$classes[$i]["cla_id"],"qus_status"=>array("in",array(1,2))))->field(array("qus_id", "qus_name"))->order("qus_create_time desc")->limit(0,5)->select();
                    }
                    $data = $this->message(1,"获取班级信息成功!",$classes);
                    unset($classes);
                } else{
                    $data = $this->message(1,"无可用班级!","");
                }
            }
            $this->ajaxReturn($data);
            unset($data,$sql);
            session_write_close();
        }
    }

    /**
     * 获取某个班级的基本情况
     */
    public function getClassInfo(){
        if(IS_AJAX && IS_LOCAL && isset($_POST["cla_id"])){
            session_start();
            $state = $_SESSION["isTeacher"];
            session_write_close();
            if($state == "true"){
                $data = $this->classPower();
            } else{
                $data = $this->stuClassPower();
            }
            if($data["status"] == 1){
                $classInfo = D()->table(array("class"=>"cla","course"=>"cou","teacher"=>"tea"))->where(array("cla.cla_id"=>$_POST["cla_id"],"__string__"=>"cla.cou_id = cou.cou_id and cou.teacher_tea_id = tea.tea_id"))->field(array("cla.cla_id", "cla.cla_name", "cla.cla_code", "cla.is_auto", "cla.cla_stu_num","cou.cou_name","cou.cou_id","tea.tea_name"))->find();
                if($classInfo){
                    $data = $this->message(1,"获取班级信息成功!",$classInfo);
                } else{
                    $data = $this->message(0,"获取班级信息失败!","");
                }
                unset($classInfo);
            }
            $this->ajaxReturn($data);
            unset($data);
        }
    }

    /**
     * 新增班级
     * ajax  cla_name   cou_id   cla_start   cla_end   is_auto
     */
    public function add(){
    	if(IS_AJAX && IS_LOCAL){
    		$sql = D();
    		$table = array("class"=>"");
    		$_POST['cla_code'] = strtoupper(substr(uniqid(), 5, 12));
    		while($sql->table($table)->where(array("cla_code"=>$_POST['cla_code']))->count() != 0){
    			$_POST['cla_code'] = strtoupper(substr(uniqid(), 5, 12));            //获取可用的班级代码
    		}
    		if($sql->table($table)->check($_POST,1)){
    			if($sql->table($table)->where(array("cla_name"=>$_POST["cla_name"],"cou_id"=>$_POST["cou_id"]))->count() == 0){
                    $d["id"] = $sql->table($table)->add($_POST);
                    if($d["id"]){
                        $course = $sql->table(array("course"=>""))->where(array("cou_id"=>$_POST["cou_id"]))->field(array("cou_name"))->find();
                        $d["cou_name"] = $course["cou_name"];
                        $data = $this->message(1,"新增班级成功!",$d);
                    } else{
                       $data = $this->message(0,"新增班级失败!","");
                   }
                   unset($d);
               } else{
                $data = $this->message(0,"同名的班级已经存在,请重新改名!","");
            }
        } else{
         $data = $this->message(0,$sql->getCheckError(),"");
     }
     $this->ajaxReturn($data);
     unset($sql,$data,$table,$course);
 }
}

    /**
     * 修改班级信息
     * ajax  cla_id   cou_id   cla_name    cla_start    cla_end   is_auto   is_filed
     */
    public function update(){
    	if(IS_AJAX && IS_LOCAL) {
    		$data = $this->classPower();
    		if ($data["status"] == 1) {
    			$sql = D();
    			$sql->startTrans();				//开启事务管理
    			$table = array("class"=>"");
    			if($sql->table($table)->check($_POST,2)){
    				if($sql->table($table)->where(array("cla_id"=>$_POST["cla_id"]))->update($_POST)){
    					if($sql->table($table)->where(array("cla_name"=>$_POST["cla_name"],"cou_id"=>$_POST["cou_id"]))->count() <= 1){
    						$sql->commit();
    						$data = $this->message(1,"班级信息修改成功!","");
    					} else{
    						$sql->rollback();
    						$data = $this->message(0,"同名的班级已经存在,请重新改名!","");
    					}
    				} else{
    					$sql->rollback();
    					$data = $this->message(0,"班级信息没有任何变化,无需修改!","");
    				}
    			} else{
    				$data = $this->message(0,$sql->getCheckError(),"");
    			}
    			unset($sql,$where);
    		}
    		$this->ajaxReturn($data);
    		unset($data);
    	}
    }

    /**
     * 检查班级是否可以删除
     * ajax  cla_id
     */
    public function check_delete(){
    	if(IS_AJAX && IS_LOCAL) {
    		$data = $this->classPower();
    		if($data["status"] == 1) {
    			$sql = D();			//连接数据库
    			$where["class_cla_id"] = $_POST["cla_id"];
    			if($sql->table(array("class_student"=>""))->where($where)->count() > 0 || $sql->table(array("question_set"=>""))->where($where)->count() > 0){
    				$data =$this->message(0,"该班级不能直接删除,该班级拥有学生或试卷!建议归档处理","");
    			} else{
    				$data = $this->message(1,"该班级可以删除!","");
    			}
    			unset($sql,$where);
    		}
    		$this->ajaxReturn($data);
    		unset($data);
    	}
    }

    /**
     * 删除班级
     * ajax  cla_id
     */
    public function delete(){
    	if(IS_AJAX && IS_LOCAL){
    		$data = $this->classPower();
    		if($data["status"] == 1) {
    			if(D()->table(array("class"=>""))->where(array("cla_id"=>$_POST["cla_id"]))->delete()){
    				$data = $this->message(1,"班级删除成功!","");
    			} else{
    				$data = $this->message(0,"班级删除失败!","");
    			}
    		}
    		$this->ajaxReturn($data);
    		unset($data);
    	}
    }

    /**
     * 恢复班级(从归档变回未归档)
     * ajax  cla_id
     */
    public function renew(){
    	if(IS_AJAX && IS_LOCAL){
    		$data = $this->classPower();
    		if($data["status"] == 1) {
    			if(D()->table(array("class"=>""))->where(array("cla_id"=>$_POST["cla_id"],"cla_end"=>array(">",date('Y-m-d',time()))))->update(array("is_filed"=>0))){
    				$data = $this->message(1,"班级恢复成功!","");
    			} else{
    				$data = $this->message(0,"班级结束时间已过,无法恢复!","");
    			}
    		}
    		$this->ajaxReturn($data);
    		unset($data);
    	}
    }

    /**
     * 归档班级(从未归档变归档)
     * ajax cla_id
     */
    public function filed(){
    	if(IS_AJAX && IS_LOCAL){
    		$data = $this->classPower();
    		if($data["status"] == 1) {
    			if(D()->table(array("class"=>""))->where(array("cla_id"=>$_POST["cla_id"]))->update(array("is_filed"=>1))){
    				$data = $this->message(1,"班级归档成功!","");
    			} else{
    				$data = $this->message(0,"班级归档失败!","");
    			}
    		}
    		$this->ajaxReturn($data);
    		unset($data);
    	}
    }


   	/**
   	 * 获得归档班级   通用  老师和学生都是调用本接口获取归档的班级信息
   	 * ajax
   	 */
   	public function filedClass(){
   		if(IS_AJAX && IS_LOCAL){
   			session_start();
   			if($_SESSION["isTeacher"] == "true"){
   				$where["cou.teacher_tea_id"] = $_SESSION["id"];
   				$where["cla.is_filed"] = 1;
   				$where["__string__"] = "cla.cou_id = cou.cou_id";
   				$class = D()->table(array("class"=>"cla","course"=>"cou"))->where($where)->field(array("cla.cla_id", "cla.cla_name", "cla.cla_stu_num", "cla.cla_start", "cla.cla_end","cou.cou_name","cou.cou_id"))->order("cla.cla_start desc")->select();
   			} else{
   				$where["cs.student_stu_id"] = $_SESSION["id"];
   				$where["cla.is_filed"] = 1;
   				$where["__string__"] = "cs.class_cla_id = cla.cla_id and cla.cou_id = cou.cou_id";
   				$class = D()->table(array("class_student"=>"cs","class"=>"cla","course"=>"cou"))->where($where)->field(array("cla_id","cla_name","cla_stu_num","cla_code","cla_start","cla_end","cou_name"))->order("cla.cla_start desc")->select();
   			}
   			unset($where);
   			if($class){
   				$data = $this->message(1,"获取归档信息成功!",$class);
   				unset($class);
   			} else{
   				$data = $this->message(0,"无归档班级!","");
   			}
   			$this->ajaxReturn($data);
   			session_write_close();
   			unset($data);
   		}
   	}

   	/**
   	 * 获得同课程下其他班级   
   	 * ajax  cou_id  cla_id
   	 */
   	public function otherClass(){
   		if(IS_AJAX && IS_LOCAL){
            $class = D()->table(array("class"=>""))->where(array("cou_id"=>$_POST["cou_id"],"cla_id"=>array("!=",$_POST["cla_id"])))->field(array("cla_id","cla_name"))->select();
            if($class){
                $data = $this->message(1,"获取其他班级信息成功!",$class);
            } else{
                $data = $this->message(0,"无其他班级信息可以获取!","");
            }
            $this->ajaxReturn($data);
            unset($data,$class);
        }
    }

    /*
    学生通过 班级名称|班级代码|课程名称|授课教师   模糊查询出课程
    ajax  本地  key关键字
    */
    public function stuSearch(){
        if(IS_AJAX && IS_LOCAL && isset($_POST["key"])){
            $where["cla.cla_name|cla.cla_code|cou.cou_name|tea.tea_name"] = array("like","%".$_POST["key"]."%");
            $where["cla.is_filed"] = 0;     //不能归档
            $where["cou.is_available"] = 1;
            $where["__string__"] = "cla.cou_id = cou.cou_id and cou.teacher_tea_id = tea.tea_id";
            $class = D()->table(array("class"=>"cla","course"=>"cou","teacher"=>"tea"))->where($where)->field(array("cla.cla_id","cla.cla_name","cla.cla_stu_num","cla.cla_code","cou.cou_name","tea.tea_name"))->select();
            if(!$class){
                $class = array();
            }
            $data = $this->message(1,"获取信息成功!",$class);
            $this->ajaxReturn($data);
            unset($where,$class,$data);
        }
    }

    /*
    app专用  班级代码进行直接到班级  无需验证或者审核
    ajax  本地  cla_code
     */
    public function appJoin(){
        if(IS_AJAX && IS_LOCAL && isset($_POST["cla_code"])){
            session_start();
            $stu_id = $_SESSION["id"];
            session_write_close();
            $sql = D();
            $class = $sql->table(array("class"=>""))->where(array("cla_code"=>$_POST["cla_code"]))->field(array("cla_id"))->find();
            if($class){
                $check = $sql->table(array("class_student"=>""))->where(array("class_cla_id"=>$class["cla_id"],"student_stu_id"=>$stu_id))->field(array("is_pass"))->find();
                if($check){
                    if($check["is_pass"] == 0){
                        if($sql->table(array("class_student"=>""))->where(array("class_cla_id"=>$class["cla_id"],"student_stu_id"=>$stu_id))->update(array("is_pass"=>1)) && $sql->table(array("class"=>""))->where(array("cla_id"=>$class["cla_id"]))->field(array("cla_stu_num"))->setInc(1)){
                            $sql->commit();
                            $data = $this->message(1,"加入班级成功!","");
                        } else{
                            $sql->rollback();
                            $data = $this->message(0,"加入班级失败!","");
                        }
                    } else{
                        $data = $this->message(0,"您已经在本班级了,不要重复加入!","");
                    }
                } else{
                    $add["class_cla_id"] = $class["cla_id"];
                    $add["student_stu_id"] = $stu_id;
                    $add["is_pass"] = 1;
                    $sql->startTrans();
                    if(is_numeric($sql->table(array("class_student"=>""))->add($add)) && $sql->table(array("class"=>""))->where(array("cla_id"=>$class["cla_id"]))->field(array("cla_stu_num"))->setInc(1)){
                        $sql->commit();
                        $data = $this->message(1,"加入班级成功!","");
                    } else{
                        $sql->rollback();
                        $data = $this->message(0,"加入班级失败!","");
                    }
                }
            } else{
                $data = $this->message(0,"不存在该班级,无法加入!","");
            }
            $this->ajaxReturn($data);
            unset($data,$sql,$class);
        }
    }

	/**
  	 * 析构函数不用写任何内容也是可以的
  	 */
	public function __destruct(){}
}
